﻿<%@ WebHandler Language="C#" Class="ContainerSuffixMaint" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

/**
 * Description: 批次后缀维护
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * create Date:2023-01-16
 */

public class ContainerSuffixMaint : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "GetCurrentUserInfo":
                    GetCurrentUserInfo(context);//查询人员信息
                    break;
                case "getCustomObjectItems":
                    getCustomObjectItems(context);//查询批次后缀
                    break;
                case "getData":
                    getData(context);//查询
                    break;
                case "saveData":
                    saveData(context);//添加后缀
                    break;
                case "removeData":
                    removeData(context);//移除后缀
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    //获取当前登录用户信息
    void GetCurrentUserInfo(HttpContext context)
    {
        if (m_UserInfo != null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "", ReturnData = m_UserInfo }));
        }
    }

    //查询批次后缀
    void getCustomObjectItems(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            string sql = @"SELECT o.customobjectitemsid,o.customobjectitemsname,o.customobjectitemsnotes,o.customobjectitemsdesc FROM customobjectitems o 
                        LEFT JOIN customobject c ON c.customobjectid =o.customobjectid WHERE o.isuse = 0 AND c.customobjectname ='批次后缀标识'";
            DataTable dt = DatabaseHelper.Query_ReturnDatTable(sql);
            foreach (DataColumn col in dt.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "获取成功", DBTable = dt }));
        }
    }
    
    //查询
    void getData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            string sql = @"
SELECT cci.containerid,cci.containername,cci.productname,cci.productdescription,cci.mfgordername,cci.mfgorderid,
       o.customobjectitemsid,o.customobjectitemsname,o.customobjectitemsnotes,o.customobjectitemsdesc,
       cci.realcontainerno,
       CASE WHEN o.customobjectitemsname IS NOT NULL THEN '原:'||cci.containername || ';' || '现:' || cci.containername || o.customobjectitemsname
            ELSE cci.containername END containernamenew
FROM containercurrentinfo cci
LEFT JOIN customobjectitems o ON o.customobjectitemsid = cci.containersuffixid
WHERE cci.containerstatus = 1 AND cci.isinstore <> -1 ";
            //主制分厂
            if (context.Request["factoryid"] != null && context.Request["factoryid"].ToString() != "")
            {
                sql += string.Format(" AND cci.originalfactoryid = '{0}'", context.Request["factoryid"].ToString());
            }
            
            //订单号
            if (context.Request["mfgordername"] != null && context.Request["mfgordername"].ToString() != "")
            {
                sql += string.Format(" AND cci.mfgordername LIKE '%{0}%'", context.Request["mfgordername"].ToString());
            }

            //件号
            if (context.Request["productname"] != null && context.Request["productname"].ToString() != "")
            {
                sql += string.Format(" AND cci.productname LIKE '%{0}%'", context.Request["productname"].ToString());
            }

            //批次号
            if (context.Request["containername"] != null && context.Request["containername"].ToString() != "")
            {
                sql += string.Format(" AND cci.containername LIKE '%{0}%'", context.Request["containername"].ToString());
            }

            //后缀标识
            if (context.Request["customobjectitemsname"] != null && context.Request["customobjectitemsname"].ToString() != "")
            {
                sql += string.Format(" AND o.customobjectitemsname LIKE '%{0}%'", context.Request["customobjectitemsname"].ToString());
            }
            
            uMESPagingDataDTO paraPage = new uMESPagingDataDTO();
            paraPage.CurrentPageIndex = Convert.ToInt32(context.Request.Form["pageNumber"].ToString());//页码
            paraPage.PageSize = Convert.ToInt32(context.Request.Form["PageSize"].ToString());//每页页数
            paraPage.strSQL = sql;
            uMESProcessResult result = DatabaseHelper.RunProcedureForPage(paraPage);
            foreach (DataColumn col in result.DBTable.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    //保存
    void saveData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            string containerid = context.Request.Form["containerid"].ToString();
            string customobjectitemsid = context.Request.Form["customobjectitemsid"].ToString();
            string sql = @"UPDATE containercurrentinfo cci SET cci.containersuffixid = '{0}' WHERE cci.containerid = '{1}'";
            sql = string.Format(sql, customobjectitemsid, containerid);
            int insertResult = DatabaseHelper.ExecuteSql(sql);
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "", DBTable = null }));
        }
    }

    //移除
    void removeData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            DataTable dtPostData = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["willBeDeleteData"].ToString());
            string sql = "";
            foreach (DataRow dr in dtPostData.Rows)
            {
                sql = string.Format("UPDATE containercurrentinfo cci SET cci.containersuffixid = '' WHERE cci.containerid = '{0}'", dr["containerid"].ToString());
                int insertResult = DatabaseHelper.ExecuteSql(sql);
            }
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "", DBTable = null }));
        }
    }
}